package com.keyancompany.zhongbaopingtai.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.CircleAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.EventBusEntity;
import com.keyancompany.zhongbaopingtai.utils.EditTextUtils;
import com.keyancompany.zhongbaopingtai.utils.T;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 创建圈子
 */
public class CreateCircleActivity extends BaseActivity {

    public static final int SELECT_PHOTOS = 222;
    @BindString(R.string.create_circle_text)
    String title;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.submit)
    TextView submit;
    @Bind(R.id.injoin_author)
    RadioGroup injoinAuthor;
    @Bind(R.id.shenhe_group)
    RadioGroup shenheGroup;
    @Bind(R.id.circle_name)
    EditText circleName;
    @Bind(R.id.circle_desc)
    EditText circleDesc;
    @Bind(R.id.circle_kouhao)
    EditText circleKouhao;
    @Bind(R.id.injoin_yes)
    RadioButton injoinYes;
    @Bind(R.id.injoin_no)
    RadioButton injoinNo;
    @Bind(R.id.shenhe_no)
    RadioButton shenheNo;
    @Bind(R.id.shenhe_yes)
    RadioButton shenheYes;

    private int isJoin = 1;

    private int isShenHe = 0;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_create_circle;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {
        injoinAuthor.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (radioGroup.getId() == R.id.injoin_yes) {
                    isJoin = 0;
                } else {
                    isJoin = 1;
                }
            }
        });

        shenheGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (radioGroup.getId() == R.id.shenhe_yes) {
                    isShenHe = 0;
                } else {
                    isShenHe = 1;
                }
            }
        });


    }

    @OnClick(R.id.submit)
    public void submitTheme(View view) {

        if (EditTextUtils.getEditTextValue(circleName) == null) {
            T.showShort(mContext, "圈子名称为必填参数！");
        }
        String nameValue = circleName.getText().toString();
        String descValue = circleDesc.getText().toString();
        String kouhaoValue = circleKouhao.getText().toString();
        if (isNetvaiable()) {
            CircleAPI.addCircleRequest("", nameValue, descValue, kouhaoValue, isJoin, isShenHe, this, getLoading(R.string.watting_hint));
        }
    }


    @Override
    public void success(int type, String json) {
        super.success(type, json);
        BaseModel base = validateJson(json);
        if (base == null || !base.isSuccess()) {
            T.showShort(mContext, base.msg);
            return;
        } else {
            T.showShort(mContext, base.msg);
            finish();
            EventBus.getDefault().post(new EventBusEntity(EventBusEntity.Type.REFRENCE));
        }

    }
}